﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;

using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;

using Kingdee.BOS.Core.Metadata.FieldElement;

using Kingdee.BOS.Orm.DataEntity;

using Kingdee.BOS.ServiceHelper;

using Kingdee.BOS.Util;

using System.ComponentModel;

using System.Linq;



namespace Jac.XkDemo.BOS.Business.PlugIn

{

    /// <summary>

    /// 【表单插件】点击单据体超链接打开单据时验证单据体明细数据权限

    /// </summary>

    [Description("【表单插件】点击单据体超链接打开单据时验证单据体明细数据权限"), HotUpdate]

    public class EntryLinkAuthDetailDataRuleFormPlugIn : AbstractDynamicFormPlugIn

    {

        public override void EntryButtonCellClick(EntryButtonCellClickEventArgs e)

        {

            base.EntryButtonCellClick(e);

            var fieldKey = "F_WNEG_Base";

            if (e.FieldKey.EqualsIgnoreCase(fieldKey))

            {

                // 获取要通过超链接打开的业务对象的数据的内码

                var field = (BaseDataField)this.View.BillBusinessInfo.GetField(e.FieldKey);

                var businessInfo = FormMetaDataCache.GetCachedFormMetaData(this.Context, field.LookUpObject.FormId).BusinessInfo;

                var val = this.View.Model.GetValue(fieldKey, e.Row) as DynamicObject;

                if (val == null || val[0] == null)

                {

                    return;

                }



                var pkid = val[0].ToString();

                // 检查当前用户是否有指定业务对象数据的明细数据的全部查看权限，权限不足时不允许打开单据查看界面

                var dataRulePara = new Kingdee.BOS.Core.Objects.Permission.Objects.FilterObjectByDataRuleParamenter(businessInfo, new[] { pkid }.ToList());

                var authResult = PermissionServiceHelper.AuthDetailDataRule(this.View.Context, dataRulePara);

                if (authResult != null && !authResult.Passed)

                {

                    this.View.ShowMessage("单据内容存在无权限明细数据，不允许查看，请从列表查看。");

                    e.Cancel = true;

                }

            }

        }

    }

}

